#import "USDashedLineView.h"

@implementation USDashedLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    if (self = [super initWithCoder:coder]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.backgroundColor = [UIColor clearColor];
    self.dashColor = [[UIColor mi_3_color] jk_darkenColor:0.1];
    self.dashWidth = 4.f;
    self.dashSpacing = 4.f;
}

- (void)prepareForInterfaceBuilder {
    [super prepareForInterfaceBuilder];
    [self setNeedsDisplay];
}

- (void)setDashColor:(UIColor *)dashColor {
    _dashColor = dashColor;
    [self setNeedsDisplay];
}

- (void)setDashWidth:(CGFloat)dashWidth {
    _dashWidth = dashWidth;
    [self setNeedsDisplay];
}

- (void)setDashSpacing:(CGFloat)dashSpacing {
    _dashSpacing = dashSpacing;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, rect.size.height);
    CGContextSetStrokeColorWithColor(context, self.dashColor.CGColor);

    CGFloat pattern[] = {self.dashWidth, self.dashSpacing};
    CGContextSetLineDash(context, 0, pattern, 2);

    CGContextMoveToPoint(context, 0, rect.size.height / 2);
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
    CGContextStrokePath(context);
}

@end
